
//登陆按钮
let signBtn = document.getElementById("SignIn");

//登陆的接口
let baseURL =  "http://www.rushmc.top/api/";


signBtn.addEventListener("click",function(){
    var str = ipt[0].value;

    // 这里是判断输入格式是否正确，正确即发送
    if(!reg0.test(str)){
        let data1 = new FormData();
    //利用构造函数创建一个对象存输入的账号密码
    var lgNameInner =document.getElementById('lg_username');
    let pwInner = document.getElementById('lg_password');
    data1.append("username",lgNameInner.value );
    data1.append("password",pwInner.value );

    
    //解决IE5 6 兼容性
    if(window.XMLHttpRequest){

        let xhr1 = new XMLHttpRequest();


        xhr1.open("POST",baseURL + "login",true);
        //设置请求类型初始化

        xhr1.send(data1);
        //发送输入的账号和密码

        xhr1.onreadystatechange = function(){
            if(xhr1.readyState === 4){
                if(xhr1.status === 200){
                    let info = JSON.parse(xhr1.responseText) 
                    //用info接受接口的数据
                    if(info.code === 200){

                        console.log(info.msg);
                        console.log(info);
                        alert("账户密码正确，即将登入...");
                        tip[0].style.display = "none";
                    }
                    else{
                      alert("账户密码错误!");
                      console.log(info.msg);
                        console.log(info);
                      lgNameInner.value="";
                      pwInner.value="";
                      tip[0].style.display = "none";
                    }
                }      
            }
        }


    }
    else{
        let xhr1 = new XMLHttpRequest();

        xhr1.open("POST",baseURL + "login",true);
        //设置请求类型初始化

        xhr1.send(data1);
        //发送输入的账号和密码

        xhr1.onreadystatechange = function(){
            if(xhr1.readyState === 4){
                if(xhr1.status === 200){
                    let info = xhr1.responseText;
                    //用info接受接口的数据
                    if(xhr1.code === 200){
                        console.log(info.msg);
                        alert("正确，即将登陆......");
                      
                    }
                    else{
                        console.log(info.msg);
                        alert("账户和密码可能错误了?");
                    }
                }      
            }
        }


    }
      
    }   
    else{
        alert("请检查用户名输入的格式！！！");
        tip[0].style.display = "block";
    }
   
})